﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace Draco.Commons.NamedObject.Configuration
{
    /// <summary>
    /// 配置加载器
    /// </summary>
    public class NamedObjectConfigurationLoader
    {
        /// <summary>
        /// 从文件加载
        /// </summary>
        /// <param name="absPath"></param>
        /// <returns></returns>
        public static List<NamedObjectItemConfiguration> Load(string absPath)
        {
            if (String.IsNullOrEmpty(absPath))
                throw new ArgumentNullException("absPath");
            if (!File.Exists(absPath))
                throw new FileNotFoundException(absPath);
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(absPath);
            return Load(xDoc);
        }
        /// <summary>
        /// 从Xml文档加载
        /// </summary>
        /// <param name="xDoc"></param>
        /// <returns></returns>
        public static List<NamedObjectItemConfiguration> Load(XmlDocument xDoc)
        {
            if(xDoc==null)
                throw new ArgumentNullException();

            List<NamedObjectItemConfiguration> list = new List<NamedObjectItemConfiguration>();

            XmlElement root = xDoc.DocumentElement;
            XmlNodeList xlist = root.SelectNodes("object");
            if (xlist != null && xlist.Count > 0)
            {
                foreach (XmlNode n in xlist)
                {
                    NamedObjectItemConfiguration item = LoadItem(n);
                    if (item != null)
                        list.Add(item);
                }
            }
            return list;
        }

        /// <summary>
        /// 加载一个配置项
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        private static NamedObjectItemConfiguration LoadItem(XmlNode xNode)
        {
            XmlAttribute xName = xNode.Attributes["name"];
            XmlAttribute xType = xNode.Attributes["type"];
            if (xName != null && !String.IsNullOrEmpty(xName.Value) && xType != null)
            {
                return new NamedObjectItemConfiguration(xName.Value, xType.Value);
            }
            return null;
        }
    }
    /// <summary>
    /// 配置项
    /// </summary>
    public class NamedObjectItemConfiguration
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 类型名称
        /// </summary>
        public string TypeName { get; set; }

        /// <summary>
        /// 生命周期
        /// </summary>
        public bool Single { get; set; }
        
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        public NamedObjectItemConfiguration(string name,string type)
        {
            if (String.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");
            Name = name;
            TypeName = type;
            Single = true;
        }
    }
    
}
